/*
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 1, or (at your option)
** any later version.

** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.

** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

/*
Date: 18.09.2002
*/

/*
* Author : Boris Mikhailenko <stg@pochtamt.ru>
*/
//---------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "common.h"
#include "packet.h"

//---------------------------------------------------------------------------
char *IntToKMG(__int64 a, int stat)
{
static __int64 M = 1024*1024;
static __int64 G = 1024*1024*1024;
static char str[30];

switch (stat)
    {
    case ST_B:
        #ifdef __WIN32__
        sprintf(str, "%Ld", a);
        #else
        sprintf(str, "%lld", a);
        #endif
        break;
    case ST_KB:
        sprintf(str, "%.2f kb", double(a)/1024.0);
        break;
    case ST_MB:
        sprintf(str, "%.2f Mb", double(a)/(1024.0*1024.0));
        break;
    default:
        sprintf(str, "%.2f kb", double(a)/1024.0);
        if (a > G)
            {
            sprintf(str, "%.2f Gb", double(a)/double(G));
            return &str[0];
            }
        if (a > M)
            {
            sprintf(str, "%.2f Mb", double(a)/double(M));
            return &str[0];
            }
        break;
    }
return &str[0];
}
//---------------------------------------------------------------------------
PAIR pair[74]= {{163,184},{164,186},{166,179},{167,191},{173,180},
				{179,168},{180,170},{182,178},{183,175},{189,165},
                {192,254},{193,224},{194,225},{195,246},{196,228},
				{197,229},{198,244},{199,227},{200,245},{201,232},
                {202,233},{203,234},{204,235},{205,236},{206,237},
                {207,238},{208,239},{209,255},{210,240},{211,241},
                {212,242},{213,243},{214,230},{215,226},{216,252},
                {217,251},{218,231},{219,248},{220,253},{221,249},
				{222,247},{223,250},{224,222},{225,192},{226,193},
				{227,214},{228,196},{229,197},{230,212},{231,195},
				{232,213},{233,200},{234,201},{235,202},{236,203},
                {237,204},{238,205},{239,206},{240,207},{241,223},
                {242,208},{243,209},{244,210},{245,211},{246,198},
				{247,194},{248,220},{249,219},{250,199},{251,216},
                {252,221},{253,217},{254,215},{255,218}};

//---------------------------------------------------------------------------
void KOIToWin(char * s2, const char * s1, int l)
{
struct PAIR
{
	int a;
	int b;
};

for (int j = 0; j < l; j++)
	{
    s2[j] = s1[j];
    for (int i = 0; i <= 73; i++)
		{
		if (s1[j] == (char)pair[i].a)
			{
			s2[j] = pair[i].b;
			break;
			}
         }
	if (s1[j] == 0)
		break;
	}
}
//---------------------------------------------------------------------------
void WinToKOI(char * s2, const char * s1, int l)
{

struct PAIR
{
	int a;
	int b;
};

for (int j = 0; j < l; j++)
	{
    s2[j] = s1[j];
    for (int i = 0; i <= 73; i++)
		{
        if (s1[j] == (char)pair[i].b)
			{
			s2[j] = pair[i].a;
			break;
			}
         }
	if (s1[j] == 0)
	    break;
	}
}
//---------------------------------------------------------------------------
void EncodePasswd(char * dst, const char * src)
{
for (int i = 0; i < PASSWD_LEN; i++)
    {
    dst[i] = src[i] ^ 49;
    }
}
//---------------------------------------------------------------------------
void DecodePasswd(char * dst, const char * src)
{
for (int i = 0; i < PASSWD_LEN; i++)
    {
    dst[i] = src[i] ^ 49;
    }
}
//---------------------------------------------------------------------------
void Encode12(char * dst, const char * src)
{
char c1, c2;
int i;
for (i = 0; ; i++)
    {
    if (src[i] == 0)
        {
        dst[i*2] = 'a';
        dst[i*2 + 1] = 'a';
        break;
        }
    c1 = src[i] & 0x0f;
    c2 = (src[i] & 0xf0) >> 4;

    c1 += 'a';
    c2 += 'a';

    dst[i*2] = c1;
    dst[i*2 + 1] = c2;
    }
dst[i*2] = 0;
}
//---------------------------------------------------------------------------
void Decode21(char * dst, const char * src)
{
char c1, c2;
int i;
for (i = 0; ; i++)
    {
    if (src[i*2] == 0)
        break;
    c1 = src[i*2];
    c2 = src[i*2 + 1];

    c1 -= 'a';
    c2 -= 'a';

    dst[i] = c1 + (c2 << 4);
    }
dst[i] = 0;
}
//---------------------------------------------------------------------------
void EncodeStr(char * str, unsigned long serial, int useHDD)
{
int len = strlen(str);
char stren[100];
int i, j = 0;
char c1, c2;
char serial_c[sizeof(serial)];
memcpy(serial_c, &serial, sizeof(serial));

for (i = 0; i < len; i++)
    {
    if (!useHDD)
        str[i] = str[i]^49;
    else
        {
        str[i] = str[i]^serial_c[j%sizeof(serial)];
        j++;
        }
    }

for (i = 0; i < 2*len; i++)
    {
    if (i%2)
        {
        c1 = (str[i/2] >> 4);
        c1 = c1 + 50;
        stren[i] = c1;
        }
    else
        {
        c2 = (str[i/2] & 0x0f);
        c2 += 50;
        stren[i] = c2;
        }
    }
stren[i] = 0;
strcpy(str, stren);
}
//---------------------------------------------------------------------------
void DecodeStr(char * str, unsigned long serial, int useHDD)
{
int len = strlen(str);
char strdc[100];
int i, j = 0;
char c1, c2;
char serial_c[sizeof(serial)];
memcpy(serial_c, &serial, sizeof(serial));

for (i = 0; i < len; i += 2)
    {
    c1 = (str[i] - 50);
    c2 = (str[i+1] - 50)<<4;
    strdc[i/2] = c1+c2;
    }
for (i = 0; i < len/2; i++)
    {
    if (!useHDD)
        strdc[i] = strdc[i]^49;
    else
        {
        strdc[i] = strdc[i]^serial_c[j%sizeof(serial)];
        j++;
        }
    }
strdc[i] = 0;
strcpy(str, strdc);
}
//---------------------------------------------------------------------------
int Min8(int a)
{
/*
Функция возвращает наименьшее число кратное 8-ми большее или равное заданному
 * */
if (a % 8 == 0)
    return a;

return a + (8 - a%8);
}
//-----------------------------------------------------------------------------
int strtod(const char *s, double &ddd)
{
    char *endptr;
    double d = strtod(s, &endptr);
    if (*endptr != 0)
    return -1;
    ddd = d;
    return 0;
}
//---------------------------------------------------------------------------

